package openrr.utils;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.Node;



public class XMLDocument {
	public Document document;
	
	public XMLDocument(String src)
	{
		try
		{
			this.loadXMLFile(src);
		} catch(DocumentException e){System.out.println("failed to load document: " + src + ", " + e.getMessage());}
	}
	public int getNumChilds(String path)
	{
		List nodes = document.selectNodes(path);
		return nodes.size();
	}
	public String getAttribute(String nodePath, String attributeName)
	{
		Node node = document.selectSingleNode(nodePath);
		if(node == null)
		{
			System.out.println("Error: an error occurred while parsing an XML file");
			return null;
		}
		return node.valueOf("@" + attributeName);
	}
	public String getTextNode(String path)
	{
		Node node = document.selectSingleNode(path);
		return node.getText();
	}
	private void loadXMLFile(String src) throws DocumentException
	{
		SAXReader xmlReader = new SAXReader();
	    this.document = xmlReader.read(src);
	}
}
